package org.jaaksi.demo.sticklist;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;

public class MyActivity extends Activity
{
	public static final int STATUE_BAR_HEIGHT = 55; // px；这里写死，代码中获取
	private View topBar;
	private View bar;
	private FrameLayout bar_container;
	private int HEIGHT;
	private boolean isStick;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 临界点距离屏幕上方高度
		HEIGHT = getResources().getDimensionPixelSize(R.dimen.title_height)
		        + STATUE_BAR_HEIGHT;

		ListView listView = (ListView) findViewById(R.id.list);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
		        android.R.layout.simple_list_item_1,
		        getResources().getStringArray(R.array.arr));
		listView.setAdapter(adapter);
		LinearLayout headView = (LinearLayout) getLayoutInflater()
		        .inflate(R.layout.view_header, null);
		bar_container = (FrameLayout) headView.findViewById(R.id.bar_container);
		topBar = headView.findViewById(R.id.top_bar);
		bar = headView.findViewById(R.id.bar);
		listView.addHeaderView(headView);

		View stick_bar = findViewById(R.id.stick_bar);

		listView.setOnScrollListener(new AbsListView.OnScrollListener()
		{
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState)
			{

			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
		            int visibleItemCount, int totalItemCount)
			{
				int[] size = new int[2];
				bar_container.getLocationInWindow(size);
				if (!isStick)
				{ // 没有悬浮
					if (size[1] < HEIGHT)
					{ // 达到悬浮位置，
						Log.i("fcy", "----------------- y=" + size[1]);
						bar_container.removeView(bar);
						stick_bar.setVisibility(View.VISIBLE);
						// 移除header中的 filterbar，添加到悬浮框中
						isStick = true;
					}
				}
				else
				{ // 悬浮中
					if (size[1] > HEIGHT)
					{
						isStick = false;
						bar_container.addView(bar);
						stick_bar.setVisibility(View.GONE);

					}
				}
			}
		});
	}
}
